package com.perfect.controller;

import com.perfect.annotation.RequirePermission;
import com.perfect.base.BaseController;
import com.perfect.base.ResultInfo;
import com.perfect.model.OrdersModel;
import com.perfect.query.OrdersQuery;
import com.perfect.service.OrdersService;
import com.perfect.vo.Post;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author YU
 */
@Controller
@RequestMapping("order")
public class OrdersController extends BaseController {

    @Resource
    private OrdersService ordersService;

    /**
     * 跳转到订单列表
     * @return
     */
    @RequirePermission(code = "705010")
    @RequestMapping("index")
    public String index(){
        return "orders/orders";
    }

    /**
     * 展示订单列表
     * @param ordersQuery
     * @return
     */
    @RequirePermission(code = "705020")
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> listOrders(OrdersQuery ordersQuery){
        return ordersService.queryByParamsForTable(ordersQuery);
    }

    /**
     * 删除订单
     * @param ids
     * @return
     */
    @RequirePermission(code = "705030")
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo delete(Integer ids){
        ordersService.deleteOrders(ids);
        return success("订单删除成功");
    }

    /**
     * 删除多个订单
     * @param ids
     * @return
     */
    @RequirePermission(code = "705040")
    @RequestMapping("deleteMore")
    @ResponseBody
    public ResultInfo deleteMore(String ids){
        ordersService.deleteMoreOrders(ids);
        return success("订单删除成功");
    }

    /**
     * 跳转到订单详情列表
     * @return
     */
    @RequirePermission(code = "705050")
    @RequestMapping("detail")
    public ModelAndView detail(Integer id){
        ModelAndView mv = new ModelAndView("orders/detail");
        OrdersModel model = ordersService.selectOrderModel(id);
        mv.addObject("model",model);
        return mv;
    }

    /**
     * 跳转到订单发货页面
     * @param id
     * @return
     */
    @RequirePermission(code = "705060")
    @RequestMapping("toSendPage")
    public ModelAndView toSendPage(Integer id){
        ModelAndView mv = new ModelAndView("orders/send");
        OrdersModel model = ordersService.selectOrderModel(id);
        mv.addObject("model",model);
        return mv;
    }

    /**
     * 订单发货
     * @param post
     * @return
     */
    @RequirePermission(code = "705070")
    @RequestMapping("send")
    @ResponseBody
    public ResultInfo send(Post post){
        System.out.println(post);
        ordersService.sendOd(post);
        return success("订单已发货");
    }
}
